package com.example.edu.Mapper;

import com.example.edu.Entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user where telephone = #{telephone}")
    User getUser(@Param("telephone") String telephone);

    List<User> getUsers(@Param("telephones") List<String> telephone);

    List<String> getNicknames(@Param("user_ids") List<Integer> user_ids);

    @Select("select telephone,nickname from user_course_student where course_id = #{course_id} order by telephone limit #{count} offset #{start}")
    List<User> getCourseUsers(@Param("course_id") int course_id, @Param("start") int start, @Param("count") int count);

    @Select("select user_id,nickname from user_course_student where course_id = #{course_id} and user_id != #{id} order by user_id")
    List<User> getClassmates(@Param("course_id") int course_id, @Param("id") int id);

    @Select("select user_id from user where telephone = #{telephone}")
    User getUserId(@Param("telephone") String telephone);

    @Select("select user_id,nickname from user where telephone = #{telephone}")
    User getUserIdAndName(@Param("telephone") String telephone);

    @Insert("insert into user(telephone, password, nickname, role) VALUES (#{user.telephone},#{user.password},#{user.nickname},#{user.role})")
    @Options(useGeneratedKeys = true, keyProperty = "user_id") // 自动获取主键
    void addUser(@Param("user") User user);

    void updateUser(@Param("telephone") String telephone,@Param("user") User user);

    @Update("update user set password = #{user.password} where telephone = #{telephone}")
    void updatePassword(@Param("telephone") String telephone,@Param("user") User user);
}
